<!DOCTYPE html>
<html lang="zxx">
<head>
    <title>FORT - 申请成为商家</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="utf-8">

    <!-- External CSS libraries -->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/animate.min.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap-submenu.css">

    <link rel="stylesheet" type="text/css" href="css/bootstrap-select.min.css">
    <link rel="stylesheet" type="text/css" href="css/magnific-popup.css">
    <link rel="stylesheet" href="css/leaflet.css" type="text/css">
    <link rel="stylesheet" href="css/map.css" type="text/css">
    <link rel="stylesheet" type="text/css" href="fonts/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="fonts/flaticon/font/flaticon.css">
    <link rel="stylesheet" type="text/css" href="fonts/linearicons/style.css">
    <link rel="stylesheet" type="text/css" href="css/jquery.mCustomScrollbar.css">
    <link rel="stylesheet" type="text/css" href="css/dropzone.css">
    <link rel="stylesheet" type="text/css" href="css/slick.css">

    <!-- Custom stylesheet -->
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" id="style_sheet" href="css/skins/default.css">

    <!-- Favicon icon -->
    <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon">


    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link rel="stylesheet" type="text/css" href="css/ie10-viewport-bug-workaround.css">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]>
    <script src="js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.min.js"></script>
    <script src="js/respond.min.js"></script>
    <![endif]-->
    <script src="js/jquery-2.2.0.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bootstrap-submenu.js"></script>
    <script src="js/rangeslider.js"></script>
    <script src="js/jquery.mb.YTPlayer.js"></script>
    <script src="js/bootstrap-select.min.js"></script>
    <script src="js/jquery.easing.1.3.js"></script>
    <script src="js/jquery.scrollUp.js"></script>
    <script src="js/jquery.mCustomScrollbar.concat.min.js"></script>
    <script src="js/leaflet.js"></script>
    <script src="js/leaflet-providers.js"></script>
    <script src="js/leaflet.markercluster.js"></script>
    <!--    <script src="js/dropzone.js"></script>-->
    <script src="js/slick.min.js"></script>
    <script src="js/jquery.filterizr.js"></script>
    <script src="js/jquery.magnific-popup.min.js"></script>
    <script src="js/jquery.countdown.js"></script>
    <script src="js/maps.js"></script>
    <!--    <script  src="js/app.js"></script>-->

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="js/ie10-viewport-bug-workaround.js"></script>
    <!-- Custom javascript -->
    <script src="js/ie10-viewport-bug-workaround.js"></script>
</head>
<body>

<!-- Main header start -->
<header class="main-header fixed-header-2">
    <div class="container">
        <nav class="navbar navbar-expand-lg navbar-light">
            <a class="navbar-brand company-logo" href="/index">
                <img src="/img/logos/black-logo.png" alt="logo">
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
                    aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav header-ml">
                    <li class="nav-item dropdown active">
                        <a class="nav-link " href="/activity" id="navbarDropdownMenuLink">
                            活动广场
                        </a>
                    </li>

                    <li class="nav-item dropdown active" id="myself">

                    </li>

                    <li class="nav-item dropdown active" id="admin_center">

                    </li>

                    <li class="nav-item dropdown megamenu-li">
                        <a class="nav-link " href="/about">关于我们</a>
                    </li>
                    <li class="nav-item dropdown megamenu-li">
                        <a class="nav-link " href="/contact">与管理员联系</a>
                    </li>
                </ul>
                <ul class="navbar-buttons ml-auto d-none d-xl-block d-lg-block">
                    <div>
                        <ul>
                            <li>
                                <div class="dropdown btns">
                                    <a class="dropdown-toggle" data-toggle="dropdown" id="status"></a>
                                    <div class="dropdown-menu">
                                        <a class="dropdown-item" href="/forgot-password">重置密码</a>
                                        <a class="dropdown-item" id="logout">退出</a>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </ul>
            </div>
        </nav>
    </div>
</header>
<!-- Main header end -->

<!-- Dashboard start -->
<div class="dashboard submit-property">
    <div class="container-fluid">
        <div class="row" style="height: 100vh">
            <div class="col-lg-2 col-md-12 col-sm-12 col-pad" >
                <div class="dashboard-nav d-none d-xl-block d-lg-block">
                    <div class="dashboard-inner">
                        <h4>账户</h4>
                        <ul>
                            <li><a href="/my-profile"><i class="flaticon-user"></i>个人中心</a></li>
                            <li class="active"><a href="/submit-property"><i class="flaticon-plus"></i>商家认证</a></li>
                        </ul>
                        <h4>活动</h4>
                        <ul>
                            <li><a href="/my-properties"><i class="flaticon-apartment"></i>我参与过的活动</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="col-lg-9 col-md-12 col-sm-12 col-pad">
                <div class="content-area5 dashboard-content">
                    <div class="dashboard-list">
                        <h3 class="heading" id="auditStatus">商家认证</h3>
                        <div class="dashboard-message contact-2 bdr clearfix">
                            <div class="row">
                                <div class="col-lg-12 col-md-12">
                                    <form action="#" method="GET" enctype="multipart/form-data">
                                        <div class="row">
                                            <div class="col-lg-6 col-md-6">
                                                <div class="form-group ">
                                                    <label>商家名</label>
                                                    <input type="text" name="storeName" class="form-control"
                                                           id="storeName">
                                                </div>
                                            </div>
                                            <div class="col-lg-6 col-md-6">
                                                <div class="form-group ">
                                                    <label>法人代表</label>
                                                    <input type="text" name="legalName" class="form-control"
                                                           id="legalName">
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-md-12 col-sm-12">
                                                <div class="form-group  ">
                                                    <label>商家简介</label>
                                                    <textarea class="form-control" name="storeInfo"
                                                              id="storeInfo"></textarea>
                                                </div>
                                            </div>

                                            <div class="col-lg-8 col-md-8 col-sm-8">
                                                <!-- Edit profile photo -->
                                                <form id="idCardForm" enctype="multipart/form-data">
                                                    <div class="change-photo-btn1">
                                                        <div class="photoUpload">
                                                            <span> 上传法人代表身份证</span>
                                                            <input type="file" class="upload" id="idCard" name="idCard"
                                                                   accept="image/*">
                                                        </div>
                                                    </div>
                                                </form>
                                                <div class="edit-profile-photo" id="idCardImg">

                                                </div>
                                            </div>

                                            <div class="col-lg-8 col-md-8 col-sm-8">
                                                <!-- Edit profile photo -->
                                                <form id="storeLicenceForm" enctype="multipart/form-data">
                                                    <div class="change-photo-btn1">
                                                        <div class="photoUpload">
                                                            <span> 上传商家营业执照</span>
                                                            <input type="file" class="upload" id="storeLicence"
                                                                   name="storeLicence"
                                                                   accept="image/*">
                                                        </div>
                                                    </div>
                                                </form>
                                                <div class="edit-profile-photo" id="storeLicenceImg">

                                                </div>
                                            </div>


                                            <div class="col-lg-5 col-md-5">
                                                <div class="send-btn">
                                                    <div class="form-group ">
                                                        <label> </label>
                                                        <button type="button" class="btn-md button-theme btn-block"
                                                                id="submit">提交
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>

                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Dashboard end -->



<script>

    const token = localStorage.getItem("token");

    $(function f() {
        //token存在
        if (token != null && token != '') {
            $.ajax({
                url: "http://localhost:5761/lottery-system-user/user/api/user/testToken",
                // url: "http://localhost:5710/user/api/user/testToken?token=" + token,
                type: "GET",
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("token", token);
                },
                success: function (result) {
                    // console.log(result);
                    if (result.code == 999) {
                        alert(result.message);
                        localStorage.removeItem("token");
                        localStorage.removeItem("userNo");
                        return window.location.href = "http://localhost/login";
                    }
                    if (result.code == 200) {
                        //正常
                        $("#myself").append("<a class='nav-link' href='/my-profile'>个人中心</a>");
                        var url = result.data.headImgUrl;
                        document.getElementById("status").innerHTML = "<img src=" + url + " alt='avatar' >" + result.data.username;
                        $("#img").attr("src", url);
                        //退出
                        $("#logout").click(function () {
                            localStorage.removeItem("token");
                            localStorage.removeItem("userNo");
                            window.location.href = "http://localhost/login";
                        });
                        //如果是商家身份，则添加菜单
                        if(result.data.userType==1){
                            $("#admin_center").append("<a class='nav-link ' href='/lottery_management' >管理中心</a>");
                        }
                        var storeUserNo = localStorage.getItem("userNo");
                        $.ajax({
                            url: "http://localhost:5761/lottery-system-user/user/api/auth/store/storeDetail/" + storeUserNo,
                            type: "GET",
                            beforeSend: function (XMLHttpRequest) {
                                XMLHttpRequest.setRequestHeader("token", token);
                            },
                            success: function (result) {
                                // console.log(result);
                                if (result.code == 200) {
                                    $("#storeName").val(result.data.storeName);
                                    $("#storeInfo").val(result.data.storeInfo);
                                    $("#legalName").val(result.data.legalName);
                                    $("#idCardImg").empty();
                                    $("#idCardImg").append("<img src=" + result.data.idCard + " alt='profile-photo' class='img-fluid' id='openIdCard'>");
                                    $("#storeLicenceImg").empty();
                                    $("#storeLicenceImg").append("<img src=" + result.data.storeLicence + " alt='profile-photo' class='img-fluid' id='openStoreLicence'>");
                                    document.getElementById("openIdCard").style.cursor = "pointer";
                                    $("#openIdCard").click(function () {
                                        window.open(result.data.idCard);
                                    });
                                    document.getElementById("openStoreLicence").style.cursor = "pointer";
                                    $("#openStoreLicence").click(function () {
                                        window.open(result.data.storeLicence);
                                    });
                                    if (result.data.auditStatus != 2) {
                                        $("#storeName").attr("disabled", "disabled");
                                        $("#storeInfo").attr("disabled", "disabled");
                                        $("#legalName").attr("disabled", "disabled");
                                        $("#idCard").attr("disabled", "disabled");
                                        document.getElementById("idCard").style.cursor = "not-allowed";
                                        $("#storeLicence").attr("disabled", "disabled");
                                        document.getElementById("storeLicence").style.cursor = "not-allowed";
                                        $("#submit").attr("disabled", "disabled");
                                        document.getElementById("submit").style.cursor = "not-allowed";
                                        if (result.data.auditStatus == 0) {
                                            document.getElementById("auditStatus").style.color = "red";
                                            document.getElementById("auditStatus").innerText = "商家认证——待审核";
                                            return;
                                        }
                                        if (result.data.auditStatus == 1) {
                                            document.getElementById("auditStatus").style.color = "red";
                                            document.getElementById("auditStatus").innerText = "商家认证——审核通过";
                                            return;
                                        }
                                    }
                                    if (result.data.auditStatus == 2) {
                                        document.getElementById("auditStatus").style.color = "red";
                                        document.getElementById("auditStatus").innerText = "商家认证——审核不通过";
                                        idCardUrl = result.data.idCard;
                                        storeLicenceUrl = result.data.storeLicence;
                                        return alert("审核不通过.由于：" + result.data.auditOpinion + "\n请重新修改");
                                    }
                                }
                            }
                        });
                        return;
                    } else {
                        //token过期或异常
                        localStorage.removeItem("token");
                        localStorage.removeItem("userNo");
                        return window.location.href = "http://localhost/login";
                    }
                }
            });
        } else {
            //token不存在
            return window.location.href = "http://localhost/login";
        }
    });

    var idCardUrl = null;
    var storeLicenceUrl = null;

    //选择完文件后就自动上传
    $("#idCard").change(function () {
        var userNo = localStorage.getItem("userNo");
        //使用 HTML5 FormData对象; 使用js原生对象
        // var file = $("#uploadForm")[0];
        var file = document.getElementById("idCard").files[0];
        var formData = new FormData();
        formData.append("userNo", userNo);
        formData.append("file", file);
        $.ajax({
            url: "http://localhost:5761/lottery-system-system/system/api/auth/upload/imag",
            // url: "http://localhost:5730/system/api/upload/headImag",
            type: 'POST',
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", token);
            },
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            async: false,
            success: function (result) {
                // console.log(result);
                if (result.code == 200) {
                    idCardUrl = result.data;
                    //预览图片
                    $("#idCardImg").empty();
                    $("#idCardImg").append("<img src=" + idCardUrl + " alt='profile-photo' class='img-fluid'>");
                    // $("#img").attr("src", url);
                }
            }
        });
    });

    //选择完文件后就自动上传
    $("#storeLicence").change(function () {
        var userNo = localStorage.getItem("userNo");
        //使用 HTML5 FormData对象; 使用js原生对象
        // var file = $("#uploadForm")[0];
        var file = document.getElementById("storeLicence").files[0];
        var formData = new FormData();
        formData.append("userNo", userNo);
        formData.append("file", file);
        $.ajax({
            url: "http://localhost:5761/lottery-system-system/system/api/auth/upload/imag",
            // url: "http://localhost:5730/system/api/upload/headImag",
            type: 'POST',
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", token);
            },
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            async: false,
            success: function (result) {
                // console.log(result);
                if (result.code == 200) {
                    storeLicenceUrl = result.data;
                    //预览图片
                    $("#storeLicenceImg").empty();
                    $("#storeLicenceImg").append("<img src=" + storeLicenceUrl + " alt='profile-photo' class='img-fluid'>");
                    // $("#img").attr("src", url);
                }
            }
        });
    });

    //点击提交
    $("#submit").click(function () {
        var storeApply = {
            userNo: localStorage.getItem("userNo"),
            storeName: $("#storeName").val(),
            storeInfo: $("#storeInfo").val(),
            legalName: $("#legalName").val(),
            idCard: idCardUrl,
            storeLicence: storeLicenceUrl
        };
        // console.log(storeApply);
        if (storeApply.storeName == '') {
            return alert("商家名不能为空");
        }
        if (storeApply.storeInfo == '') {
            return alert("商家简介不能为空");
        }
        if (storeApply.legalName == '') {
            return alert("法人代表不能为空");
        }
        if (storeApply.idCard == null) {
            return alert("请上传法人代表身份证");
        }
        if (storeApply.storeLicence == null) {
            return alert("请上传商家营业执照");
        }
        $.ajax({
            url: "http://localhost:5761/lottery-system-user/user/api/auth/store/apply",
            type: "POST",
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", token);
            },
            contentType: "application/json;charset=UTF-8",
            dataType: "JSON",
            data: JSON.stringify(storeApply),
            success: function (result) {
                // console.log(result);
                if (result.code == 100) {
                    return alert(result.message);
                }
                alert(result.message);
                window.location.reload();
            }
        });

    });

</script>


</body>
</html>